'use strict'

define(['jquery', 'bootstrap', 'shiftform'], function($, validate) {
  
  return {
    
    init: function() {
      this.confirm();
      this.initPage();
    },
    
    initPage: function() {
      
      $('a.btn-dispatch').addClass('disabled');
      
      $(document).on('click', '#select-all', function() {
        var $selectAll = $('#select-all');
        $('input[name="loadNumber"]').prop('checked', $selectAll.is(':checked'));
      });
      
      $(document).on('change', '#select-all, input[name="loadNumber"]', function(e) {
        var count = $('input[name="loadNumber"]:checked').length;
        if (count > 0) {
          $('a.btn-dispatch').removeClass('disabled');
        } else {
          $('a.btn-dispatch').addClass('disabled');
        }
      });
      
    },
    
    confirm: function(){
      
      $(document).on("shown.bs.modal", "#dispatching-confirm", function (e) {
          
        var loadNumbers = new Array();
        $('input[name="loadNumber"]:checked').each(function() {
          var loadNumber = $(this).val();
          loadNumbers.push(loadNumber);
        });
        $(e.currentTarget).find('input[name="loadNumbers"]').val(loadNumbers);
        
        var $form = $(e.currentTarget).find("form");                          //bind shiftform
        $form.shiftform();
        
      });
      
    }

  }  
})